package jmine.tec.hydra.services.incluir;

import java.nio.charset.Charset;

import jmine.tec.database.pack.PackException;
import jmine.tec.database.pack.services.PackEntityPersistenceHelper;
import jmine.tec.di.annotation.Injected;
import jmine.tec.hydra.HydraMessages;
import jmine.tec.hydra.domain.HydraSystem;
import jmine.tec.hydra.exception.HydraException;
import jmine.tec.services.annotations.Execution;
import jmine.tec.services.annotations.Input;
import jmine.tec.services.annotations.Output;
import jmine.tec.services.annotations.ServiceImplementor;
import jmine.tec.services.api.ActionsEnum;
import jmine.tec.services.api.export.ReferenceMap;
import jmine.tec.services.api.export.ServiceFiller;
import jmine.tec.services.export.impl.ExportUtils;
import jmine.tec.services.io.ServiceBean;

/**
 * Serviço para inclusão de um sistema de integração.
 * 
 * @author piercio
 */
@ServiceImplementor(action = ActionsEnum.INCLUIR)
public class HydraSystemService implements ServiceFiller<HydraSystem> {

    public static final String IDENTIFICADOR = "Identificador";

    public static final String MNEMONICO = "Mnemônico";

    public static final String ENCODING = "Encoding";

    public static final String DATABASE_ID = "Database ID";

    // Injected
    private PackEntityPersistenceHelper helper;

    // Obrigatório
    private String mnemonico;

    private String encoding;

    // Opcional
    private Long databaseID;

    /**
     * Executa o serviço e cria um {@link HydraSystem}
     * 
     * @return o sistema de integração.
     * @throws PackException PackException
     */
    @Execution
    @Output(propertyName = IDENTIFICADOR)
    public HydraSystem executeService() throws PackException {
        if (!Charset.isSupported(this.encoding)) {
            throw new HydraException(HydraMessages.SYSTEM_UNSUPPORTED_ENCODING.create(this.mnemonico, this.encoding));
        }

        HydraSystem system = this.helper.findOrCreate(HydraSystem.class, this.databaseID, this.mnemonico);
        system.setMnemonico(this.mnemonico);
        system.setEncoding(this.encoding);
        this.helper.save(system, this.databaseID);

        return system;
    }

    /**
     * {@inheritDoc}
     */
    public void fillServiceBean(ServiceBean bean, ReferenceMap referenceMap, HydraSystem entity) {
        ExportUtils.setService(bean, this.getClass());
        ExportUtils.put(bean, MNEMONICO, entity.getMnemonico());
        ExportUtils.put(bean, ENCODING, entity.getEncoding());
        ExportUtils.putDatabaseId(bean, DATABASE_ID, entity);
        ExportUtils.put(bean, IDENTIFICADOR, referenceMap.addReference(entity, "hydraSystem"));
    }

    /**
     * @param helper the helper to set
     */
    @Injected
    public void setHelper(PackEntityPersistenceHelper helper) {
        this.helper = helper;
    }

    /**
     * @param mnemonico o mnemonico do sistema.
     */
    @Input(fieldName = MNEMONICO)
    public void setMnemonico(String mnemonico) {
        this.mnemonico = mnemonico;
    }

    /**
     * @param encoding o valor para o campo encoding
     */
    @Input(fieldName = ENCODING, defaultValue = "UTF-8")
    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }

    /**
     * @param databaseID the databaseID to set
     */
    @Input(fieldName = DATABASE_ID, required = false)
    public void setDatabaseID(Long databaseID) {
        this.databaseID = databaseID;
    }
}
